package com.chocho.producerserver.dao;

import com.chocho.producerserver.bean.QCollection;
import com.chocho.producerserver.bean.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by chocho on 2022/3/20.
 */
@Mapper
@Repository
public interface UserDao {

    //根据用户名查询用户信息
    @Select("SELECT user_id, user_name, user_pwd, user_status FROM op_user WHERE user_name = #{username}")
    public User selectByUsername(String username);

    //根据用户Id查询用信息
    @Select("SELECT user_id, user_name, user_pwd, user_status FROM op_user WHERE user_id = #{userId}")
    public User selectUserById(String userId);

    //插入用户信息
    @Insert("INSERT INTO op_user (user_id, user_name, user_pwd, user_status) " +
            "VALUES(#{userId}, #{userName}, #{userPwd}, 1)")
    public int insertUser(User user);

    @Select("SELECT * FROM op_user " +
            "<where>" +
            "<trim suffixOverrides='and'> " +
            "<if test='username != '' and username != null'> user_name = #{userName} </if> " +
            "</trim> " +
            "</where>")
    List<User> selectByLike(Map<String, Object> map);

    @Insert("INSERT INTO op_user " +
            "<trim prefix='(' suffix=')' suffixOverrides=',' > " +
            "<if test='userId != null'>user_id, </if> " +
            "<if test='userName != null' >user_name, </if>" +
            "<if test='userPwd != null' >user_pwd, </if>" +
            "<if test='userStatus != null' >user_status, </if>" +
            "</trim> " +
            "<trim prefix='values (' suffix=')' suffixOverrides=',' > " +
            "<if test='userId != null' > #{userId,jdbcType=CHAR}, </if>" +
            "<if test='userName != null' > #{userName,jdbcType=VARCHAR}, </if>" +
            "<if test='userPwd != null' > #{userPwd,jdbcType=CHAR}, </if>" +
            "<if test='userStatus != null' > #{userStatus,jdbcType=TINYINT}, </if>" +
            "</trim>")
    Integer insertNotNull(User user);

    @Update("UPDATE op_user " +
            "<set> " +
            "<if test='userId != null'> user_id = #{userId,jdbcType=CHAR}, </if> " +
            "<if test='userName != null'> user_name = #{userName,jdbcType=VARCHAR}, </if> " +
            "<if test='userPwd != null'> user_pwd = #{userPwd,jdbcType=VARCHAR}, </if> " +
            "<if test='userStatus != null'> user_status = #{userStatus,jdbcType=TINYINT}, </if> " +
            "</set> " +
            "WHERE user_id = #{userId,jdbcType=CHAR} ")
    Integer updateByIDNotNUll(User user);

    @Delete("DELETE FROM op_user WHERE user_id = #{id}")
    Integer deleteByID(String id);

    @Update("UPDATE op_user SET user_pwd = #{userPwd} WHERE user_id = #{userId}")
    void updatePassword(Map<String, Object> map);

    @Select("SELECT COUNT(user_id) FROM op_user")
    int getMaxNum();

    @Select("<script>" +
            "SELECT * FROM op_user " +
            "<where>" +
            "<trim suffixOverrides='and'> " +
            "<if test=\"userId != \'\' and userId != null\"> user_id like CONCAT('%',#{userId},'%')</if> " +
            "</trim> " +
            "<trim suffixOverrides='and'> " +
            "<if test=\"userName != \'\' and userName != null\"> user_name like CONCAT('%',#{userName},'%')</if> " +
            "</trim> " +
            "<trim suffixOverrides='and'> " +
            "<if test=\"userStatus != \'\' and userStatus != null\"> user_status like CONCAT('%',#{userStatus},'%')</if> " +
            "</trim> " +
            "</where>" +
            "</script>")
    List<User> getUserByQuery(Map<String, Object> map);

    @Delete("<script>" +
            "DELETE FROM op_user WHERE user_id in " +
            "<foreach collection=\"list\" open=\"(\" close=\")\" separator=\",\"  item=\"item\"> #{item}" +
            "</foreach>" +
            "</script>")
    int delete(List<String> idList);

    @Update("UPDATE op_user SET user_pwd = #{pwd} WHERE user_id = #{id}")
    int updatePwd(String id, String pwd);
}
